package com.gqz.utils;


import com.gqz.domain.SysUser;
import jdk.nashorn.internal.objects.annotations.Setter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户信息工具类
 */
public class UserTools {

    //获取当前登录用户的ID
    public static Long getCurrentUserId(HttpServletRequest request) {
        SysUser userInfo = getUserInfo(request);
        return userInfo.getId();
    }

    //获取当前登录用户的真实姓名
    public static String getCurrentUserRealName(HttpServletRequest request) {
        SysUser userInfo = getUserInfo(request);
        return userInfo == null ? null : userInfo.getRealName();
    }


    //把用户信息存入session
    public static void saveUserSession(HttpServletRequest request, SysUser user) {
        HttpSession session = request.getSession();
        System.err.println("sessionId 1 =" + session.getId());
        session.setAttribute(Constants.LOGIN_USER_INFO, user);
    }

    //从session中获取用户信息
    public static SysUser getUserInfo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.err.println("sessionId 2 =" + session.getId());
        return (SysUser) session.getAttribute(Constants.LOGIN_USER_INFO);
    }

    public static void clearUserSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        //System.err.println("sessionId 3 =" + session.getId());
        session.removeAttribute(Constants.LOGIN_USER_INFO);
    }
}